package de.clapp.backend.network;

import java.io.IOException;
import java.net.Socket;

import com.google.gson.Gson;

import de.clapp.backend.requests.ServerRequest;

public class ClientReply implements AutoCloseable
{
    private TCPSocket socket;

    private Gson gson;

    public ClientReply(TCPSocket socket) throws IOException
    {
        this.socket = socket;
        this.gson = new Gson();
    }

    public ServerRequest getRequest() throws IOException
    {
        // return gson.fromJson((String) this.socket.receive(),
        // ServerRequest.class);
        //

        return (ServerRequest) this.socket.receive();
    }

    public void answer(Object obj) throws IOException
    {
        // this.socket.send(obj);

        // Gson alternative:
        this.socket.send(gson.toJson(obj));
    }
    
    public Socket getSocket()
    {
        return this.socket.getSocket();
    }

    @Override
    public void close() throws Exception
    {
        // TODO Auto-generated method stub
        this.socket.close();
    }
}
